package fr.expdev.bndd.data.sqlite;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import fr.expdev.bndd.data.Post;
import fr.expdev.bndd.data.sqlite.BNDDSQLite.PostColumn;
import fr.expdev.bndd.data.sqlite.BNDDSQLite.Table;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class PostsSQLite extends ModelSQLite {
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public PostsSQLite(Context context) {
		super(context);
	}

	/**
	 * Insertion d'un post dans la base de données
	 * @param post Post a insérer
	 */
	public void insert(Post post) {
		ContentValues content = new ContentValues();
		content.put(PostColumn.POST_TITLE.toString(), post.getTitle());
		content.put(PostColumn.POST_PUBLICATIONDATE.toString(), dateFormat.format(post.getPublicationDate()));
		content.put(PostColumn.POST_EXCERPT.toString(), post.getExcerpt());
		content.put(PostColumn.POST_LINK.toString(), post.getLink());
		content.put(PostColumn.POST_TYPE.toString(), post.getType().toString());
		content.put(PostColumn.POST_FAVORITE.toString(), post.isFavorite()?1:0);
		
		long id = db.insert(Table.POSTS.toString(), null, content);
		post.setId(id);
	}
	
	/**
	 * Mise à jour d'un enregistrement
	 * @param post Post à mettre à jour
	 */
	public void update(Post post) {
		if (post.getId() > 0) {
			ContentValues content = new ContentValues();
			content.put(PostColumn.POST_TITLE.toString(), post.getTitle());
			content.put(PostColumn.POST_PUBLICATIONDATE.toString(), dateFormat.format(post.getPublicationDate()));
			content.put(PostColumn.POST_EXCERPT.toString(), post.getExcerpt());
			content.put(PostColumn.POST_LINK.toString(), post.getLink());
			content.put(PostColumn.POST_TYPE.toString(), post.getType().toString());
			content.put(PostColumn.POST_FAVORITE.toString(), post.isFavorite()?1:0);
			
			db.update(Table.POSTS.toString(), content, PostColumn.POST_ID + "=" + post.getId(), null);
		}
	}
	
	/**
	 * Récupération des posts dans l'ordre du plus récent au plus ancien
	 * @return La liste des posts trouvés en base de données
	 */
	public List<Post> selectAllPosts() {
		Cursor c = db.query(Table.POSTS.toString(), null, null, null, null, null, null);
		List<Post> posts = new ArrayList<Post>();
		
		if (c.getCount() == 0) {return posts;}
		
		while(c.moveToNext()) {
			try {
				Post p = new Post();
				p.setId(c.getInt(PostColumn.POST_ID.getId()));
				p.setTitle(c.getString(PostColumn.POST_TITLE.getId()));
				p.setPublicationDate(dateFormat.parse(c.getString(PostColumn.POST_PUBLICATIONDATE.getId())));
				p.setExcerpt(c.getString(PostColumn.POST_EXCERPT.getId()));
				p.setLink(c.getString(PostColumn.POST_LINK.getId()));
				p.setType(c.getString(PostColumn.POST_TYPE.getId()));
				p.setFavorite(c.getInt(PostColumn.POST_FAVORITE.getId()) == 1);
				
				posts.add(p);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		} 
		
		
		c.close();
		
		return posts;
	}
	
}
